package com.example.administrator.blapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity extends AppCompatActivity {

    private EditText mEditText;
    private TextView mTextView_left;
    private RadioGroup mRadioGroup_country;
    private CheckBox mCheckBox_eat, mCheckBox_sleep;

    private Button mButton_load_img;
    private ImageView mImageView_girl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        mEditText = (EditText)findViewById(R.id.ip_num);
        mTextView_left = findViewById(R.id.tv_left);
        mRadioGroup_country = findViewById(R.id.rg_country);

        mCheckBox_eat = findViewById(R.id.cb_eat);
        mCheckBox_sleep = findViewById(R.id.cb_sleep);

        mImageView_girl = findViewById(R.id.iv_girl);
        mButton_load_img = findViewById(R.id.bt_load_image);

        TextView tv = findViewById(R.id.tv);
        tv.setSelected(true);
        tv.setHorizontallyScrolling(true);

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mTextView_left.setText("还可以输入: "+(11 - charSequence.length())+" 个数字");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mRadioGroup_country.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                RadioButton button = findViewById(i);
                Toast.makeText(TestActivity.this, button.getText(), Toast.LENGTH_SHORT).show();
            }
        });


        mCheckBox_eat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(TestActivity.this, compoundButton.getText().toString() + ":" + b, Toast.LENGTH_SHORT).show();
            }
        });

        mButton_load_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mImageView_girl.setImageResource(R.mipmap.logo);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void bohaoClick(View v){
      //  Toast.makeText(this, "ddd"+String.valueOf(v.getId()),Toast.LENGTH_LONG).show();
        String phoneNum = mEditText.getText().toString();
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(("tel:" + phoneNum)));

        if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        startActivity(intent);
    }

    public void cleanup(View v){
        mEditText.setText("");
    }
}
